package com.dhcc.sds.standard.controller;


import java.util.List;
import java.util.Map;
import java.util.Objects;


import cn.hutool.core.bean.BeanUtil;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import com.dhcc.sds.standard.service.ISdsStandDicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.standard.entity.SdsStandOrderMatchEntity;
import com.dhcc.sds.standard.service.ISdsStandOrderMatchService;


/**
 * 标准化_医嘱对照
 * 
 * @ClassName: SdsStandOrderMatchController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:09:56
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstandordermatch")
public class SdsStandOrderMatchController extends BaseController {

	private String PREFIX = "/backend/sds/sdsstandordermatch/";

	@Autowired
	private ISdsStandOrderMatchService sdsStandOrderMatchService;

    @Autowired
    private ISdsStandDicService sdsStandDicService;
	

    /**
     * 跳转到标准化_医嘱对照列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("dicMap",sdsStandDicService.getDicMapBySource("抗菌药物"));
        return PREFIX + "sdsstandordermatch.html";
    }
	
    /**
     * 跳转到新增标准化_医嘱对照
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsstandordermatch_add.html";
    }
	
	/**
     * 跳转到修改标准化_医嘱对照
     */
    @RequestMapping("/to_update/{sdsStandOrderMatchId}")
    public String toUpdate(@PathVariable Long sdsStandOrderMatchId, Model model) {
        SdsStandOrderMatchEntity sdsStandOrderMatch = this.sdsStandOrderMatchService.selectById(sdsStandOrderMatchId);
        model.addAttribute("sdsStandOrderMatch",sdsStandOrderMatch);
        return PREFIX + "sdsstandordermatch_edit.html";
    }

    /**
     * 获取标准化_医嘱对照列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params, @RequestParam String compare) {
		Page<SdsStandOrderMatchEntity> page = new PageFactory<SdsStandOrderMatchEntity>().defaultPage();
        List<SdsStandOrderMatchEntity> result = this.sdsStandOrderMatchService.page(page,params,compare);
        page.setRecords((List<SdsStandOrderMatchEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增标准化_医嘱对照
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsStandOrderMatchEntity sdsStandOrderMatch) {
        if (CommonUtil.isOneEmpty(sdsStandOrderMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsStandOrderMatch.insert();
        return R.ok();
    }

    /**
     * 删除标准化_医嘱对照
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsStandOrderMatchId) {
	
        this.sdsStandOrderMatchService.deleteById(sdsStandOrderMatchId);

        return R.ok();
    }

    /**
     * 修改标准化_医嘱对照
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsStandOrderMatchEntity sdsStandOrderMatch) {
        if (CommonUtil.isOneEmpty(sdsStandOrderMatch)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsStandOrderMatchEntity old = this.sdsStandOrderMatchService.selectById(sdsStandOrderMatch.getId());

        old.setDicsubId(sdsStandOrderMatch.getDicsubId());
        old.setMatchType(sdsStandOrderMatch.getMatchType());
        old.setOrdmastId(sdsStandOrderMatch.getOrdmastId());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 同步医嘱项到标准化_医嘱对照表
     */
    @RequestMapping(value = "/sync")
    @ResponseBody
    public Object sync() {

        this.sdsStandOrderMatchService.sync();

        return R.ok();
    }

    /**
     * 撤销标准化_医嘱对照
     */
    @RequestMapping(value = "/undo")
    @ResponseBody
    public Object undo(@RequestParam("ordmastIds") String ordmastIds, @RequestParam Long sdsStandDicsubId, @RequestParam Long sdsStandDicId) {
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        this.sdsStandOrderMatchService.undo(ordmastIds, sdsStandDicsubId, sdsStandDicId, username);

        return R.ok();
    }

    /**
     * 标准化_医嘱对照
     */
    @RequestMapping(value = "/compare")
    @ResponseBody
    public Object compare(@RequestParam Long sdsStandDicsubId, @RequestParam("ordmastIds") String ordmastIds, @RequestParam Long sdsStandDicId) {
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        this.sdsStandOrderMatchService.compare(sdsStandDicsubId, ordmastIds, sdsStandDicId, username);

        return R.ok();
    }


    /**
     * 自动匹配标准化_医嘱对照
     */
    @RequestMapping(value = "/autoCompare")
    @ResponseBody
    public Object autoCompare() {

        this.sdsStandOrderMatchService.autoCompare();

        return R.ok();
    }


    /**
     * 获取医嘱项字典表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listOrdmast")
    @ResponseBody
    public Object listOrdmast(@RequestParam Map<String, Object> params, @RequestParam String compare) {
        Page<CdcOrdmastEntity> page = new PageFactory<CdcOrdmastEntity>().defaultPage();
        List<CdcOrdmastDto> result = this.sdsStandOrderMatchService.pageOrdmast(page,params,compare);

        Page<CdcOrdmastDto> dtoPage = new PageFactory<CdcOrdmastDto>().defaultPage();
        BeanUtil.copyProperties(page,dtoPage);
        dtoPage.setRecords(result);
        return R.packForBT(dtoPage);
    }


    /**
     * 获取医嘱项字典表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listMatchOrdmast")
    @ResponseBody
    public Object listMatchOrdmast(@RequestParam Map<String, Object> params, @RequestParam String compare) {

        if (Objects.equals("match", compare)){
            Page<SdsStandOrderMatchEntity> page = new PageFactory<SdsStandOrderMatchEntity>().defaultPage();
            List<CdcOrdmastDto> result = this.sdsStandOrderMatchService.pageMatchOrdmast(page,params,compare);

            Page<CdcOrdmastDto> dtoPage = new PageFactory<CdcOrdmastDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcOrdmastEntity> page = new PageFactory<CdcOrdmastEntity>().defaultPage();
            List<CdcOrdmastDto> result = this.sdsStandOrderMatchService.pageOrdmast(page,params,compare);

            Page<CdcOrdmastDto> dtoPage = new PageFactory<CdcOrdmastDto>().defaultPage();
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }

    }

    /**
     * 对照的标准字典进行渲染
     */
    @RequestMapping(value = "/matchDicsub")
    @ResponseBody
    public Object matchDicsub() {

        List<Long> matchIds = this.sdsStandOrderMatchService.selectMatchDicsub();

        return matchIds;
    }

}
